using UnityEngine;
using System.Collections;

/**
 * Script of menu keyboard control description.
 * The purpose of this class is to control menus with keyboard.
 * @author Benjamin Bruneau
 */
public class MenuControllerScript : MonoBehaviour 
{

	public GameObject _selectedChild; //Selected 3DText
	public GameObject _leaveChild; // Quit 3DText
	public GameObject _menuSelectSound; // Menu Select Sound GameObject
	
	
	
	/**
	 * Called once per frame.
	 * @return void
	 */
	void Update () 
	{
		
		// Return key : forward
		if (Input.GetKeyUp(KeyCode.Return))
		{
			_selectedChild.GetComponent<DetectClickOnObjectScript>().forwardScene();		
		}
		
		// Escape key : back in menus
		if (Input.GetKeyUp(KeyCode.Escape))
		{
			_leaveChild.GetComponent<DetectClickOnObjectScript>().forwardScene();		
		}
		
		//Directions : navigate in menus
		if (Input.GetKeyUp(KeyCode.UpArrow))
		{
			GameObject newChild = _selectedChild.GetComponent<DetectClickOnObjectScript>()._top;
			while (!newChild.GetComponent<DetectClickOnObjectScript>().enabled && (!newChild.name.Equals(_selectedChild.name))) {
				newChild = newChild.GetComponent<DetectClickOnObjectScript>()._top;	
			}
			navigateInMenus (newChild);
			_menuSelectSound.GetComponent<AudioSource>().Play();

		}
		
		if (Input.GetKeyUp(KeyCode.DownArrow))
		{
			GameObject newChild = _selectedChild.GetComponent<DetectClickOnObjectScript>()._bottom;
			while (!newChild.GetComponent<DetectClickOnObjectScript>().enabled && (!newChild.name.Equals(_selectedChild.name))) {
				newChild = newChild.GetComponent<DetectClickOnObjectScript>()._bottom;	
			}
			navigateInMenus (newChild);
			_menuSelectSound.GetComponent<AudioSource>().Play();

		}
		
		if (Input.GetKeyUp(KeyCode.LeftArrow))
		{
			GameObject newChild = _selectedChild.GetComponent<DetectClickOnObjectScript>()._left;
			while (!newChild.GetComponent<DetectClickOnObjectScript>().enabled && (!newChild.name.Equals(_selectedChild.name))) {
				newChild = newChild.GetComponent<DetectClickOnObjectScript>()._left;	
			}
			navigateInMenus (newChild);
			_menuSelectSound.GetComponent<AudioSource>().Play();

		}
		
		if (Input.GetKeyUp(KeyCode.RightArrow) )
		{
			GameObject newChild = _selectedChild.GetComponent<DetectClickOnObjectScript>()._right;
			while (!newChild.GetComponent<DetectClickOnObjectScript>().enabled && (!newChild.name.Equals(_selectedChild.name))) {
				newChild = newChild.GetComponent<DetectClickOnObjectScript>()._right;	
			}
			navigateInMenus (newChild);
			_menuSelectSound.GetComponent<AudioSource>().Play();
		}	
	}
	
	/**
	 * Called to navigate in menus and manage selections.
	 * @param GameObject
	 * @return void
	 */
	private void navigateInMenus (GameObject pNewChild)
	{
		_selectedChild.GetComponent<DetectClickOnObjectScript>()._selected = false;
		pNewChild.GetComponent<DetectClickOnObjectScript>()._selected = true;
		_selectedChild = pNewChild;
	}
}
